<?php

namespace App\Http\Controllers\ChinaBank\Admin;

use App\Models\ChinaBank\GoodsModel;
use App\Models\ChinaBank\ShopModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    public function goods(Request $request){

        $goods  = GoodsModel::with('shopinfo')->paginate(15);
        $params = [];
        return self::adminview("goods/goods-list",['goods'=>$goods,'params'=>$params]);
    }

    public function goodsEdit(Request $request){

        $id     = $request->input('id',0);

        if($request->method() == 'POST')
        {
            $data   = $request->all(["content","price","name","note","shop","images","thumb_img"]);
            if($id > 0){
                GoodsModel::where('id',$id)->update($data);
            }else{
                GoodsModel::create($data);
            }
            return self::send();
        }

        if($id > 0){
            $goods  = GoodsModel::find($id);
        }else{
            $goods  = new \stdClass();
            $goods->name = "";
            $goods->id = 0;
            $goods->price = 0;
            $goods->images = "";
            $goods->thumb_img = "";
            $goods->note = "";
            $goods->content = "";
            $goods->shop = 0;
        }
        $brands = ShopModel::get();
        return self::adminview("goods/goods-edit",['goods'=>$goods,'brands'=>$brands]);
    }

    public function upload(Request $request){
        $file   = $request->file("file");
        $ext    = $file->getClientOriginalExtension();
        $path   = $file->getRealPath();
        $urlpath = "upload/chinabank/".time().rand(1000,9999).".".$ext;
        $savePath = public_path($urlpath);
        move_uploaded_file($path,$savePath);
        return self::send(['img'=>$urlpath]);
    }
}
